{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python 日期和时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间。\n",
    "\n",
    "时间间隔是以秒为单位的浮点小数。\n",
    "\n",
    "每个时间戳都以自从1970年1月1日午夜（历元）经过了多长时间来表示。\n",
    "\n",
    "Python 的 time 模块下有很多函数可以转换常见日期格式。如函数time.time()用于获取当前时间戳, \n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前时间戳： 1563432531.8498442\n",
      "获取当前时间 location_time：  time.struct_time(tm_year=2019, tm_mon=7, tm_mday=18, tm_hour=14, tm_min=48, tm_sec=51, tm_wday=3, tm_yday=199, tm_isdst=0)\n",
      "获取格式化的时间  Thu Jul 18 14:48:51 2019\n",
      "格式化日期： 2019-07-18 14:48:51\n",
      "2019-7的月历 ：\n",
      "      July 2019\n",
      "Mo Tu We Th Fr Sa Su\n",
      " 1  2  3  4  5  6  7\n",
      " 8  9 10 11 12 13 14\n",
      "15 16 17 18 19 20 21\n",
      "22 23 24 25 26 27 28\n",
      "29 30 31\n",
      "\n",
      "10971.358292216\n",
      "11.640625\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Anaconda3_installpath\\envs\\tensorflow\\lib\\site-packages\\ipykernel_launcher.py:41: DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import calendar\n",
    "\n",
    "ticks  = time.time()\n",
    "\n",
    "print('当前时间戳：',ticks )\n",
    "\n",
    "\n",
    "\n",
    "# 获取当前时间\n",
    "location_time = time.localtime(time.time())\n",
    "print('获取当前时间 location_time： ',location_time)\n",
    "\n",
    "\n",
    "\n",
    "# 获取格式化的时间\n",
    "asctime = time.asctime(location_time)\n",
    "print('获取格式化的时间 ',asctime)\n",
    "\n",
    "\n",
    "'''\n",
    "\n",
    "'%Y-%M-%D %H:%M:%S'    2019-01-07/18/19 12:01:09\n",
    "\n",
    "\n",
    "'''\n",
    "\n",
    "# 格式化日期\n",
    "strf_location = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())\n",
    "print('格式化日期：',strf_location)\n",
    "\n",
    "\n",
    "\n",
    "# 获取某月日历\n",
    "\n",
    "cal2019_7 = calendar.month(2019,7)\n",
    "print('2019-7的月历 ：\\n',cal2019_7)\n",
    "\n",
    "\n",
    "# 用以浮点数计算的秒数返回当前的CPU时间。用来衡量不同程序的耗时，比time.time()更有用\n",
    "clockTime = time.clock()\n",
    "print(clockTime)\n",
    "\n",
    "\n",
    "process_time = time.process_time()\n",
    "print(process_time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
